package com.murphy.algorithm.likou;

/**
 * @author dongsufeng
 * @version 4.0
 * @date 2023/2/20 17:29
 */
public class LinkedSort {

    public ListNode sort(ListNode l){
        ListNode next ;
        while (l!=null){
            next = l.next;
            while (next != null){
                if (l.val>next.val){
                    int temp = l.val;
                    l.val=next.val;
                    next.val=temp;
                }
                next=next.next;
            }
            l=l.next;
        }
        return l;
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(3);
        l1.next = new ListNode(23);
        l1.next.next=new ListNode(9);
        l1.next.next.next=new ListNode(12);
        LinkedSort sort = new LinkedSort();
        ListNode newnode = sort.sort(l1);
        while (newnode!=null){
            System.out.println(newnode.val);
            newnode = newnode.next;
        }
    }

    public static class ListNode{
        private int val;
        private ListNode next;
        public ListNode(){}
        public ListNode(int val){
            this.val = val;
        }
    }
}
